import { defineConfig, loadEnv } from "vite";
import { resolve } from "path";
import vue from "@vitejs/plugin-vue";
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

export default defineConfig(({ mode }) => {
  let env = loadEnv(mode, process.cwd());
  return {
    base: env.VITE_USER_NODE_ENV === 'production' ? './' : '/',
    server: {
      hmr: true,
      host: "0.0.0.0",
      port: "9999",
      open: true,
      https: false,
      proxy: {
        [env.VITE_APP_BASE_API]: {
          target: env.VITE_SERVE,
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, '')
        },
      }
    },
    resolve: {
      alias: {
        "@": resolve(__dirname, "./src"),
      },
      // alias: {
      //   "@": path.resolve("./src"),
      // },
    },
    css: {
      preprocessorOptions: {
        scss: {
          // javascriptEnabled: true,
          additionalData: '@import "@/style/root.scss";',
        },
      },
    },
    // 分包
    build: {
      rollupOptions: {
        manualChunks(id) {
          if (id.includes("node_modules")) {
            return "vendor";
          }
        },
      },
    },
    plugins: [
      Components({
        resolvers: [ElementPlusResolver()],
      }),
      vue(),
      AutoImport({
        include: [/\.vue$/, /\.vue\?vue/],
        imports: [
          "vue",
          "vue-router",
          "pinia",
          {
            "@/utils/enums.js": ["ENUMS"],
            "element-plus": ["ElMessage"],
            "@/hooks/use/index.js": [["default", "_hook"]],
          },
        ],
        eslintrc: {
          enabled: false,
        },
      }),
    ],
  }
})
